package Gui.Dialogs;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import Gui.DiceImagePanel;
import Gui.GameFrame;
import Main.Defines;
import PlayerManager.Chance;

public class LotteryDayDialog extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = 9097306974805062545L;
	private static final int LOTTERY_TICKET_COST = 100;
	private JPanel jp;

	public void buildUIContents() {
		// set info label
		JLabel lotteryInfo = new JLabel(
				"<html>Please select the users who want "
						+ "<br></br>to participate in the lottery. </html>");
		jp.add(lotteryInfo, BorderLayout.NORTH);

		// show player checkboxes
		final ArrayList<JCheckBox> boxList;
		JPanel checkBoxPanel = new JPanel();
		checkBoxPanel.setLayout(new GridLayout(0, 1));
		ArrayList<JCheckBox> localBoxList = new ArrayList<JCheckBox>();
		for (int i = 0; i < Defines.PLM.getPlayerList().size(); ++i) {
			JCheckBox playerBox = new JCheckBox(String.format("Player %d",
					i + 1));
			checkBoxPanel.add(playerBox);
			localBoxList.add(playerBox);
		}
		jp.add(checkBoxPanel, BorderLayout.CENTER);
		boxList = localBoxList;
		// show ok button
		JButton ok = new JButton("OK");
		ok.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
				GameFrame.getInstance().update();
				boolean someoneParticipating = false;
				for (JCheckBox a : boxList) {
					if (a.isSelected())
						someoneParticipating = true;
				}
				if (someoneParticipating) {
					@SuppressWarnings("unused")
					LotterDayDialogAfterMath showDialogAfterMath = new LotterDayDialogAfterMath(
							boxList);
				}

			}
		});
		jp.add(ok, BorderLayout.SOUTH);
	}

	public LotteryDayDialog() {
		//super(GameFrame.getInstance(), "Lottery Dialog", true);
		setModalityType(getModalityType().APPLICATION_MODAL);
		// setLayout(new GridLayout(0, 1, 0, 5));
		jp = new JPanel();
		jp.setBorder(Defines.getEmptyBorderConstraints());
		jp.setLayout(Defines.getHeightStackedBorderLayout());
		
		
		buildUIContents();
		add(jp);
		setResizable(false);
		pack();

		if (getTitle() != null) {
			FontMetrics fm = getFontMetrics(getFont());
			int width = fm.stringWidth(getTitle()) + Defines.ICON_WIDTH; 
			width = (int) Math.max(width * 1.2, getPreferredSize().getWidth());
			setSize(new Dimension(width, getHeight()));
		}
		setLocationRelativeTo(GameFrame.getInstance());
		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		setVisible(true);
		

	}

	private class LotterDayDialogAfterMath extends JDialog {
		/**
		 * 
		 */
		private static final long serialVersionUID = -4902015949994512579L;
		private JPanel jp;

		private void buildUIContents(ArrayList<JCheckBox> boxList) {
			Chance chance = new Chance();

			boolean aPlayerWonLotto = false;
			// set so eclipse won't complain that it hasnt been initialized
			int whichPlayerWon = -1;
			// this makes sure a player is playing so that Defines.LOAN_AMOUNT
			// is added to the
			// lottery pile
			boolean atLeastOnePlayerIsPlaying = false;
			String allText = "";
			JLabel allTextLabel = new JLabel();

			for (int i = 0; i < boxList.size(); ++i) {
				if (boxList.get(i).isSelected()) {
					atLeastOnePlayerIsPlaying = true;
					if (Defines.PLM.getPlayerList().get(i).getCash() < LOTTERY_TICKET_COST) {
						// maybe show dialog asking if they want to loan

						AskForLoanDialog showDialog = new AskForLoanDialog(i,
								LOTTERY_TICKET_COST, " for the lottery ticket",
								Defines.LOAN_AMOUNT);
						if (showDialog.isClosed() && showDialog.isLoanOK()) {
							Defines.PLM.getPlayerList().get(i).AddCash(
									-LOTTERY_TICKET_COST);
							Defines.GM.addToLotteryPile(LOTTERY_TICKET_COST);
							Defines.PLM.getPlayerList().get(i).AddCash(
									Defines.LOAN_AMOUNT);
							Defines.PLM.getPlayerList().get(i).AddLoan(
									Defines.LOAN_AMOUNT);
						}
					}
					else {
						// remove 100 dollars from their cash and add to lottery
						Defines.PLM.getPlayerList().get(i).AddCash(
								-LOTTERY_TICKET_COST);
						Defines.GM.addToLotteryPile(LOTTERY_TICKET_COST);
					}
					// check iterated player's lotto card if it has a matching
					// 2d roll
					if (Defines.PLM.getPlayerList().get(i).getLottoCard()
							.contains(chance)) {
						aPlayerWonLotto = true;
						whichPlayerWon = i;
					}

				}
			}
			if (atLeastOnePlayerIsPlaying) {
				// Defines.LOAN_AMOUNT dollars is added to pile from bank
				Defines.GM.addToLotteryPile(Defines.LOAN_AMOUNT); 
				// if a player won the lottery, print it, add lottery money and
				// set pile to 0

				// JLabel lottoRoll = new
				// JLabel(String.format("The Lotto Roll is %s",
				// chance.toString()));
				allText += String.format("<html>The Lotto Roll is %s<br>",
						chance.toString());
				// add(lottoRoll);

				JPanel rollPics = new JPanel();
				try {
					rollPics.add(new DiceImagePanel().getDicePanel(false,
							chance.getFirst()));
					rollPics.add(new DiceImagePanel().getDicePanel(true, chance
							.getSecond()));
					jp.add(rollPics, BorderLayout.NORTH);
				}

				catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				if (aPlayerWonLotto) {
					allText += String.format("<br>Player %d WON $%d<br>",
							whichPlayerWon + 1, Defines.GM.getLotteryPile());
					Defines.PLM.getPlayerList().get(whichPlayerWon).AddCash(
							Defines.GM.getLotteryPile());
					Defines.GM.setLotteryPile(Defines.LOTTERY_PILE_RESET_VAL);
					GameFrame.getInstance().update();
				}
				else {
					allText += String
							.format("No one won the lottery this week.<br>"
									+ "");
					allText += String.format("<br>Jackpot value: %d<br>",
							Defines.GM.getLotteryPile());
					GameFrame.getInstance().update();
				}

			}
			allTextLabel.setText(allText);
			jp.add(allTextLabel, BorderLayout.CENTER);

			JButton ok = new JButton("OK");
			ok.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					dispose();

				}
			});
			jp.add(ok, BorderLayout.SOUTH);

		}

		public LotterDayDialogAfterMath(ArrayList<JCheckBox> boxList) {
			//super(GameFrame.getInstance(), "Lottery Dialog", true);
			jp = new JPanel();
			jp.setLayout(Defines.getHeightStackedBorderLayout());
			jp.setBorder(Defines.getEmptyBorderConstraints());
			setLocationRelativeTo(GameFrame.getInstance());
			buildUIContents(boxList);
			add(jp);

			pack();
			if(getTitle() != null){
				FontMetrics fm = getFontMetrics(getFont());
				int width = fm.stringWidth(getTitle()) + Defines.ICON_WIDTH;
				width = (int) Math.max(width*1.2, getPreferredSize().getWidth());
				setSize(new Dimension(width, getHeight()));
			}
			setVisible(true);
			setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

		}
	}
}
